package com.cl.spark.nodenew;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cl.spark.base.BaseSparkNode;
import com.cl.spark.dto.SparkParam;
import com.cl.spark.dto.SparkResult;
import com.cl.spark.enums.SparkNodeEnum;
import com.cl.spark.util.OKHttpUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class HttpRequestNode extends BaseSparkNode {

    @Override
    public SparkResult process(SparkParam sparkParam) {
        JSONArray res = new JSONArray();
        List<JSONArray> dataList = sparkParam.getDataList();
        JSONObject expressionObj = sparkParam.getNodeExpression();
        JSONArray data = dataList.get(0);
        String method = expressionObj.getString("method");
        String url = expressionObj.getString("url");
        String paramTemplate = expressionObj.getString("paramTemplate");
        String resultPosition = expressionObj.getString("resultPosition");


        for (int i = 0; i < data.size(); i++) {
            try {
                Pattern pattern = Pattern.compile("\\$\\{(.+?)}");
                Matcher matcher = pattern.matcher(paramTemplate);

                Object resItem = new Object();
                JSONObject item = data.getJSONObject(i);
                String paramString = paramTemplate;

                while (matcher.find()) {
                    String fieldName = matcher.group(1);
                    paramString = paramString.replace(matcher.group(0), item.getString(fieldName));
                }

                String resString = "";
                if ("POST".equals(method)) {
                    resString = OKHttpUtil.okhttpPostString(url, JSONObject.parseObject(paramString));
                } else if ("GET".equals(method)) {
                    resString = OKHttpUtil.okhttpGetString(url, JSONObject.parseObject(paramString));
                }
                JSONObject resObj = JSONObject.parseObject(resString);
                if (resultPosition.contains(".")) {
                    String[] split = resultPosition.split("\\.");
                    JSONObject resItemObj = JSONObject.parseObject(resString);
                    for (int j = 0; j < split.length; j++) {
                        if (j == split.length - 1) {
                            resItem = resItemObj.get(split[i]);
                        } else {
                            resItemObj = resItemObj.getJSONObject(split[i]);
                        }
                    }
                } else if (StringUtils.hasLength(resultPosition)) {
                    resItem = resObj.get(resultPosition);
                } else {
                    resItem = resObj;
                }
                String resItemJSON = JSON.toJSONString(resItem);
                if (JSON.isValidArray(resItemJSON)) {
                    JSONArray jsonArray = JSONArray.parseArray(resItemJSON);
                    for (int j = 0; j < jsonArray.size(); j++) {
                        JSONObject itemCopy = JSONObject.parseObject(item.toJSONString());
                        itemCopy.putAll(jsonArray.getJSONObject(j));
                        res.add(itemCopy);
                    }
                } else {
                    item.putAll(JSONObject.parseObject(resItemJSON));
                    res.add(item);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return SparkResult.success(res);
    }

    @Override
    public SparkNodeEnum getType() {
        return SparkNodeEnum.HTTP_REQUEST;
    }
}
